<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>七牛云存储测试页面</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background-color: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }
        .form-group {
            margin-bottom: 20px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #555;
        }
        input[type="file"], input[type="text"] {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        button {
            background-color: #007bff;
            color: white;
            padding: 12px 24px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            margin-right: 10px;
            margin-bottom: 10px;
        }
        button:hover {
            background-color: #0056b3;
        }
        .result {
            margin-top: 20px;
            padding: 15px;
            border-radius: 4px;
            white-space: pre-wrap;
            font-family: monospace;
        }
        .success {
            background-color: #d4edda;
            border: 1px solid #c3e6cb;
            color: #155724;
        }
        .error {
            background-color: #f8d7da;
            border: 1px solid #f5c6cb;
            color: #721c24;
        }
        .info {
            background-color: #d1ecf1;
            border: 1px solid #bee5eb;
            color: #0c5460;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>七牛云存储测试页面</h1>
        
        <!-- 文件上传 -->
        <div class="form-group">
            <label for="fileInput">选择文件：</label>
            <input type="file" id="fileInput" accept="*/*">
        </div>
        
        <div class="form-group">
            <label for="fileName">文件名（可选）：</label>
            <input type="text" id="fileName" placeholder="不填写则自动生成">
        </div>
        
        <div class="form-group">
            <label for="directory">存储目录（可选）：</label>
            <input type="text" id="directory" placeholder="例如：images/">
        </div>
        
        <button onclick="uploadFile()">上传文件</button>
        <button onclick="getFileList()">获取文件列表</button>
        <button onclick="getDirectories()">获取目录列表</button>
        <button onclick="testApi()">测试API</button>
        
        <div id="result"></div>
    </div>

    <script>
        const API_BASE = 'http://localhost:8080/api/storage';
        
        function showResult(message, type = 'info') {
            const resultDiv = document.getElementById('result');
            resultDiv.className = `result ${type}`;
            resultDiv.textContent = message;
        }
        
        async function uploadFile() {
            const fileInput = document.getElementById('fileInput');
            const fileName = document.getElementById('fileName').value;
            const directory = document.getElementById('directory').value;
            
            if (!fileInput.files[0]) {
                showResult('请选择要上传的文件', 'error');
                return;
            }
            
            const formData = new FormData();
            formData.append('file', fileInput.files[0]);
            if (fileName) formData.append('fileName', fileName);
            if (directory) formData.append('directory', directory);
            
            try {
                showResult('正在上传文件...', 'info');
                
                const response = await fetch(`${API_BASE}/upload`, {
                    method: 'POST',
                    body: formData
                });
                
                const result = await response.json();
                
                if (result.code === 200) {
                    showResult(`上传成功！\n${JSON.stringify(result.data, null, 2)}`, 'success');
                } else {
                    showResult(`上传失败：${result.message}`, 'error');
                }
            } catch (error) {
                showResult(`上传异常：${error.message}`, 'error');
            }
        }
        
        async function getFileList() {
            const directory = document.getElementById('directory').value;
            
            try {
                showResult('正在获取文件列表...', 'info');
                
                let url = `${API_BASE}/files`;
                if (directory) {
                    url += `?directory=${encodeURIComponent(directory)}`;
                }
                
                const response = await fetch(url);
                const result = await response.json();
                
                if (result.code === 200) {
                    showResult(`文件列表：\n${JSON.stringify(result.data, null, 2)}`, 'success');
                } else {
                    showResult(`获取失败：${result.message}`, 'error');
                }
            } catch (error) {
                showResult(`获取异常：${error.message}`, 'error');
            }
        }
        
        async function getDirectories() {
            try {
                showResult('正在获取目录列表...', 'info');
                
                const response = await fetch(`${API_BASE}/directories`);
                const result = await response.json();
                
                if (result.code === 200) {
                    showResult(`目录列表：\n${JSON.stringify(result.data, null, 2)}`, 'success');
                } else {
                    showResult(`获取失败：${result.message}`, 'error');
                }
            } catch (error) {
                showResult(`获取异常：${error.message}`, 'error');
            }
        }
        
        async function testApi() {
            try {
                showResult('正在测试API...', 'info');
                
                const response = await fetch(`${API_BASE}/test`);
                const result = await response.json();
                
                if (result.code === 200) {
                    showResult(`测试成功：${result.data}`, 'success');
                } else {
                    showResult(`测试失败：${result.message}`, 'error');
                }
            } catch (error) {
                showResult(`测试异常：${error.message}`, 'error');
            }
        }
    </script>
</body>
</html>